<?php

    class Product extends database_object{
        
        public $id;
        public $name;
        public $description;
        public $user_id;
        
        protected static $table_name = "bo_product";
        protected static $db_fields = array("id", "name", "description", "user_id");
        
        
        //Method builds a product object
        public function build($cregisterproduct="",$cregisteruser_id=""){
                $product            = new Product();
                $product->name      = $cregisterproduct;
                $product->user_id   = $cregisteruser_id;
                $product->save();
                return $this->id;
        }
        
        //Method checks if the product name exists in database
        public function productExists($name){
                $database   = Database::getInstance();
                $name       =  $database->escape_value($name);
                $sql        = "SELECT * FROM " . self::$table_name;
                $sql       .= " WHERE name= '{$name}' ;";

               $result_array = self::find_by_sql($sql);
                        
               return !empty($result_array) ? array_shift($result_array) : false;
        }
       
        
      
           //Method returns array of stats items. One for the amount of males, one for the amount of females. 
        public function getAverageRatePerSex($id){
        
            $totalrate = array();
            $sql = "SELECT AVG( rate )
                    FROM bo_rating, bo_user
                    WHERE product_id ={$id}
                    AND user_id = bo_user.id
                    AND bo_user.sex = 'male'";

            $sql = "SELECT AVG( rate )
                    FROM bo_rating, bo_user
                    WHERE product_id ={$id}
                    AND user_id = bo_user.id
                    AND bo_user.sex = 'female'";
            
            $male      = new StatsItem('male', $totalrate[]     = self::executeSQL($sql));
            $female    = new StatsItem('female', $totalrate[]   = self::executeSQL($sql2)); 
            
            $totalrate[] = $male;
            $totalrate[] = $female;
            
            return $totalrate;
        
        
        }
        
        public function toString(){
            
            return $this->name;
            
        }
       
        
   
}
$product=  new Product();

?>
